/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.serverpackets;

import javolution.util.FastMap;

import com.aionemu.gameserver.configs.main.SiegeConfig;
import com.aionemu.gameserver.model.siege.SiegeLocation;
import com.aionemu.gameserver.model.team.legion.Legion;
import com.aionemu.gameserver.model.team.legion.LegionEmblem;
import com.aionemu.gameserver.network.aion.AbstractAionServerPacket;
import com.aionemu.gameserver.network.aion.GameClient;
import com.aionemu.gameserver.services.SiegeService;

/**
 * @author Lyahim, Sarynth - packets from rhys2002.
 *
 */

public class SM_SIEGE_LOCATION_INFO extends AbstractAionServerPacket<GameClient>
{
	/***
	 * infoType
	 *  0 - reset
	 *  1 - change
	 */
	private final int								infoType;

	private final FastMap<Integer, SiegeLocation>	locations;

	public SM_SIEGE_LOCATION_INFO()
	{
		this.infoType = 0; // Reset
		locations = SiegeService.getInstance().getSiegeLocations();
	}

	/**
	 * @param loc
	 */
	public SM_SIEGE_LOCATION_INFO(SiegeLocation loc)
	{
		this.infoType = 1; // Update
		locations = new FastMap<Integer, SiegeLocation>();
		locations.put(loc.getLocationId(), loc);
	}

	@Override
	protected void writeImpl(GameClient client)
	{
		if (SiegeConfig.SIEGE_ENABLED == false)
		{
			// Siege is Disabled
			writeC(0);
			writeH(0);
			return;
		}

		writeC(infoType);
		writeH(locations.size());
		for (FastMap.Entry<Integer, SiegeLocation> e = locations.head(), end = locations.tail(); (e = e.getNext()) != end;)
		{
			SiegeLocation sLoc = e.getValue();

			writeD(sLoc.getLocationId()); // Artifact ID
			int legionId = sLoc.getLegionId();
			writeD(legionId); // Legion ID
			if (legionId != 0)
			{
				LegionEmblem legionEmblem = Legion.getLegionById(legionId).getLegionEmblem();
				writeC(legionEmblem.getVersion());
				writeC(legionEmblem.isUserEmblem()? 0x80 : 0x00);
				writeH(0);//timer ;)
				writeD(legionEmblem.getColor_rgb());
			}
			else
			{
				writeH(0);
				writeH(0);
				writeD(0);
			}
			writeC(sLoc.getRace().getRaceId());

			// is vulnerable (0 - no, 2 - yes)
			writeC(sLoc.isVulnerable() ? 2 : 0);

			// faction can teleport (0 - no, 1 - yes)
			writeC(1);

			// Next State (0 - invulnerable, 1 - vulnerable)
			writeC(sLoc.getNextState());

			writeD(0); // unk
			writeD(0); // unk 1.9 only
		}
	}
}
